package com.team.book.web;

import com.team.book.common.domain.Page;
import com.team.book.common.domain.ResponseEntity;
import com.team.book.common.exception.UncheckedException;
import com.team.book.domain.Book;
import com.team.book.domain.BookClassification;
import com.team.book.service.BookService;
import com.team.book.web.dto.BookDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.method.P;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 图书
 * @author Joann
 *
 */
@RestController
@RequestMapping(value = "/api/book")
public class BookController {

	@Autowired
	public BookService bookService;

    @GetMapping()
    public ResponseEntity<Page<BookDTO>> query(Page<BookDTO> page, BookDTO book){
    	Page<BookDTO> bookPage = bookService.query(page,book);
        return ResponseEntity.ok(bookPage);
    }

    /**
     * 获取书籍信息
     * @param code
     * @return
     */
    @GetMapping(value= "/{code}")
    public ResponseEntity<Book> getBook(@PathVariable long code){
        try{
            Book book = bookService.get(code);
            return ResponseEntity.ok(book);
        } catch (UncheckedException e) {
            return ResponseEntity.set(e.getStatus());
        }
    }

    /**
     * 添加
     * @param book
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Void> save(@RequestBody @Valid Book book){
        int num = bookService.save(book);
        if(num>0){
            return ResponseEntity.ok();
        }
        return ResponseEntity.fail();
    }

    /**
     * 修改
     * @param book
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Void> update(@RequestBody @Valid Book book){
        try{
            bookService.update(book);
            return ResponseEntity.ok();
        } catch (UncheckedException e) {
            return ResponseEntity.set(e.getStatus());
        }
    }

    /**
     * 删除书籍
     * @param book
     * @return
     */
    @RequestMapping(value = "/{code}",method = RequestMethod.DELETE,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Void> del(@PathVariable Long code){
        try{
            bookService.del(code);
            return ResponseEntity.ok();
        } catch (UncheckedException e) {
            return ResponseEntity.set(e.getStatus());
        }
    }

    /**
     * 结束
     * @param book
     * @return
     */
    @RequestMapping(value = "/borrow",method = RequestMethod.PUT,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Void> borrow(@RequestBody Book book){
        try {
            bookService.borrow(book.getCode());
            return ResponseEntity.ok();
        } catch (UncheckedException e) {
            return ResponseEntity.set(e.getStatus());
        }
    }

    /**
     * 还书
     * @param book
     * @return
     */
    @RequestMapping(value = "/return-book",method = RequestMethod.PUT,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Void> returnBook(@RequestBody Book book){
        try {
            bookService.returnBook(book.getCode());
            return ResponseEntity.ok();
        } catch (UncheckedException e) {
            return ResponseEntity.set(e.getStatus());
        }
    }

    /**
     * 添加分类
     * @param classification
     * @return
     */
    @RequestMapping(value = "/classification",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Void> classification(@RequestBody @Valid BookClassification classification){
        int num = bookService.saveClassification(classification);
        if(num>0){
            return ResponseEntity.ok();
        }
        return ResponseEntity.fail();
    }

    /**
     * 查询分类
     * @return
     */
    @RequestMapping(value = "/classification",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<BookClassification>> queryClassification(){
        List<BookClassification> classification = bookService.queryClassification();
        return ResponseEntity.ok(classification);
    }

    /**
     * 修改分类
     * @return
     */
    @RequestMapping(value = "/classification",method = RequestMethod.PUT,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Void> updateClassification(@RequestBody @Valid BookClassification classification){
        try {
            bookService.updateClassification(classification);
        } catch (UncheckedException e) {
            return ResponseEntity.set(e.getStatus());
        }
        return ResponseEntity.ok();
    }

    /**
     * 删除分类
     * @return
     */
    @RequestMapping(value = "/classification/{id}",method = RequestMethod.DELETE,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Void> delClassification(@PathVariable Long id){
        try {
            BookClassification classification = new BookClassification();
            classification.setId(id);
            bookService.delClassification(classification);
        } catch (UncheckedException e) {
            return ResponseEntity.set(e.getStatus());
        }
        return ResponseEntity.ok();
    }
}
